import service from "@/utils/request";
import { ElMessage } from "element-plus";
import { getToken } from "@/utils/jwt";

//获取题目列表
export async function getExamListService(params) {
  let token = getToken();
  console.log("token" + token);
  return service
    .get("/exam/list", {
      headers: {
        "Admin-Oj-b-Token": token, // 登陆令牌
      },
      params: params, // 查询参数
    })
    .then((res) => {
      if (res.code == 1000) {
        console.log(res.rows);
        return res;
      } else {
        ElMessage({
          message: res.msg,
          type: "error",
        });
        return [];
      }
    })
    .catch((error) => {
      console.log("error");
      return [];
    });
}
//新增竞赛
export async function examAddService(exam) {
  console.log("examAddService:{}", exam);
  return service({
    url: "/exam/add",
    method: "post",
    data: exam,
  });
}

//竞赛新增题目
export async function addExamQuestionService(examQ) {
  return service({
    url: "/exam/questionAdd",
    method: "post",
    data: examQ,
  });
}

//竞赛新增题目
export async function getExamDetailService(examId) {
  let token = getToken();
  let obj = {
    examId: examId,
  };
  console.log("token" + token);
  return service
    .get("/exam/getExamDetailsById", {
      headers: {
        "Admin-Oj-b-Token": token, // 登陆令牌
      },
      params: obj, // 查询参数
    })
    .then((res) => {
      return res;
    })
    .catch((error) => {
      console.log("error");
    });
}

//删除竞赛下面的题目
export async function delExamQuestionService(examQ) {
  return service({
    url: "/exam/questionDelete",
    method: "post",
    data: examQ,
  });
}

export async function publishExamService(examId) {
  return service({
    url: "/exam/publish",
    method: "post",
    params: { examId },
  });
}
export async function cancelPublishExamService(examId) {
  return service({
    url: "/exam/unpublish",
    method: "post",
    params: { examId },
  });
}
